using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using TCEntity;

namespace TCClientService
{
  public static class CommunityService
  {
    /// <summary>
    /// 加入社区。
    /// </summary>
    /// <param name="client">HTTP客户端。</param>
    /// <param name="user">给定用户。</param>
    /// <param name="communityId">社区Id。</param>
    /// <param name="success">成功时执行的方法。</param>
    /// <param name="failure">失败时执行的方法，传入失败原因字符串。</param>
    /// <param name="InternetFailure">网络错误时执行的方法。</param>
    /// <returns></returns>

    public static async Task JoinAsync(this HttpClient client, User user, int communityId, Action success, Action<string> failure, Action InternetFailure)
    {
      var request = new HttpRequestMessage(HttpMethod.Post, "./Community/join");
      await TCHttpService.SendAsync<object>(client, request, (user, communityId), _ => success(), failure, InternetFailure);
    }
    /// <summary>
    /// 按社区名查找社区。未找到社区时视为查找失败。
    /// </summary>
    /// <param name="client">HTTP客户端。</param>
    /// <param name="user">给定用户。</param>
    /// <param name="communityName">社区名称。</param>
    /// <param name="success">成功时执行的方法，传入结果社区。</param>
    /// <param name="failure">失败时执行的方法，传入失败原因字符串。</param>
    /// <param name="InternetFailure">网络错误时执行的方法。</param>
    /// <returns></returns>
    public static async Task SearchAsync(this HttpClient client, User user, string communityName, Action<Community> success, Action<string> failure, Action InternetFailure)
    {
      var request = new HttpRequestMessage(HttpMethod.Get, "./Community/search");
      await TCHttpService.SendAsync(client, request, (user, communityName), success, failure, InternetFailure);
    }
    /// <summary>
    /// 查找用户加入的所有社区。
    /// </summary>
    /// <param name="client">HTTP客户端。</param>
    /// <param name="user">给定用户。</param>
    /// <param name="success">成功时执行的方法，传入结果社区列表。</param>
    /// <param name="failure">失败时执行的方法，传入失败原因字符串。</param>
    /// <param name="InternetFailure">网络错误时执行的方法。</param>
    /// <returns></returns>
    public static async Task SelectAsync(this HttpClient client, User user, Action<List<Community>> success, Action<string> failure, Action InternetFailure)
    {
      var request = new HttpRequestMessage(HttpMethod.Get, "./Community/select");
      await TCHttpService.SendAsync(client, request, user, success, failure, InternetFailure);
    }
    /// <summary>
    /// 退出社区。
    /// </summary>
    /// <param name="client">HTTP客户端。</param>
    /// <param name="user">给定用户。</param>
    /// <param name="communityId">社区Id。</param>
    /// <param name="success">成功时执行的方法。</param>
    /// <param name="failure">失败时执行的方法，传入失败原因字符串。</param>
    /// <param name="InternetFailure">网络错误时执行的方法。</param>
    /// <returns></returns>
    public static async Task QuitAsync(this HttpClient client, User user, int communityId, Action success, Action<string> failure, Action InternetFailure)
    {
      var request = new HttpRequestMessage(HttpMethod.Delete, "./Community/quit");
      await TCHttpService.SendAsync<object>(client, request, (user, communityId), _ => success(), failure, InternetFailure);
    }

  }
}
